const schedule = require('node-schedule');
const moment = require("moment")
const { EquipmentModel } = require("../models")
const mqttController = require("./mqtt")

class ScheduleController {

  // 判断并修正设备状态
  judgeMachiceStatus  = async () => {

    // 设备最终读数半天未响应时进行修改其设备状态
    let query = {
      status: 1,
      machine_status: 1,
      real_time: {$lte: moment().add(-12, "hours").toDate()}
    };
    let result = await EquipmentModel.updateMany(query, {
      $set: {
        machine_status: 3
      }
    })
    // 若修改的数值数大于0 则对其设备缓存进行更新
    if(result.n > 0){
      mqttController.getEquipmentData();
    }
    
  }


  lift = () => {
    console.log("start schedule job")
    // 1小时检查一次
    schedule.scheduleJob('0 0 * * * *', this.judgeMachiceStatus);
  }

}


module.exports = new ScheduleController()
